﻿/**
* @author Børge Bergstrøm
* @description This javascript file defines the behaviour of the scrumboard
*/
/// <reference path="jquery-1.7.2.js" />

//Global variables
var selectedSprintVal = 0;

//Exexute when the DOM is ready
$(document).ready(function () {
    // JQuery Approach:
    //----------------
    // Set draggable attribute on .story
    $('.story').draggable({
        revert: true,
        revertDuration: 0
    });

    $(".phase").droppable({
        accept: ".story",
        drop: function (event, ui) {
            var draggable = ui.draggable;
            $(this).last("div").append(draggable); //Append current draggable to droppable
            resizePhases();

            /*
            *   update userstory phase
            */
            //get userstory ID
            var uStID = $(draggable).last(".storyheader").text().match(/\d+/).toString();

            // Get phaseName
            var phaseName = $(this).find(".phasetitle").text();

            //set new phase on dropped userstory in database

            $.ajax({
                url: "../UpdateUserStoryPhase",
                data: { usID: uStID, phaseName: phaseName },
                type: "POST"
            });
        }
    });
});
/*
* Helper functions
*/

function showHideInfoarea() {
    var openleft = '▲ open'
    var openright = 'open ▲'
    var closeleft = '▼ close'
    var closeright = 'close ▼'
    var element = document.getElementById('infoarea');
    if (element.style.display == 'none' || element.style.display == '') {
        element.style.display = 'block';
        document.getElementById('infobartooltipleft').innerHTML = closeleft;
        document.getElementById('infobartooltipright').innerHTML = closeright;
    }
    else {
        element.style.display = 'none';
        document.getElementById('infobartooltipleft').innerHTML = openleft;
        document.getElementById('infobartooltipright').innerHTML = openright;
    }
    resizePhases();
}

function resizePhases() {
    var navigation = document.getElementById('navigation');
    var infobar = document.getElementById('infobar');
    var infoarea = document.getElementById('infoarea');
    var elementHeight = 0;
    if (infoarea.style.display != 'none') {
        elementHeight += infoarea.offsetHeight;
    }
    elementHeight += navigation.offsetHeight;
    elementHeight += infobar.offsetHeight;
    var phaseHeight = window.innerHeight - elementHeight;

    var phases = document.getElementById('phases');
    phases.style.minHeight = phaseHeight + 'px';

    var divs = document.getElementsByTagName('div');

    var phaseClasses = new Array();

    for (var i = 0; i < divs.length; i++) {
        if (divs[i].className.indexOf('phase phaseodd') > -1 || divs[i].className.indexOf('phase phaseeven') > -1) {
            if (maxOffset > phaseHeight) {
                divs[i].style.minHeight = maxOffset + 'px';
                phaseClasses.push(divs[i]);
            }
            else {
                divs[i].style.minHeight = phaseHeight + 'px';
                phaseClasses.push(divs[i]);
            }
            //divs[i].style.marginTop = navigation.offsetHeight + 'px';
        }
    }

    var maxOffset = 0;
    for (var i = 0; i < phaseClasses.length; i++) {
        if (document.getElementById('phase' + (i + 1)).offsetHeight > maxOffset) {
            maxOffset = document.getElementById('phase' + (i + 1)).offsetHeight;
        }
        phaseClasses[i].style.width = (100 / phaseClasses.length) + "%";
    }

    
}

window.onresize = function () {

    resizePhases();
}